<?php
namespace app\admin\controller;
use app\common\model\StoreTruck as StoreTruckModel;
use app\common\model\StoreClassify as StoreClassifyModel;
use app\common\controller\AdminBase;
use think\Config;
use think\Db;
/**
 * 管理
 * Class Adminstoretruck
 * @package app\admin\controller
 * 默认(品牌ID:1,价格ID:2,车型ID:3,驱动ID:4)
 */
class StoreTruck extends AdminBase
{
    protected $storetruck_model;
    protected $storeclassify_model;
    protected function _initialize()
    {
        parent::_initialize();
        $this->storetruck_model = new StoreTruckModel();
        $this->storeclassify_model = new StoreClassifyModel();
        $store_list = $this->storeclassify_model->select();
        $store_classify = $this->storeclassify_model->column('name','id');
        $area_list = Db('area')->select();
        $this->assign('store_list', $store_list);
        $this->assign('store_classify', $store_classify);
        $this->assign('area_list',$area_list);
    }
    /**
     * 管理
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function index($type = '',$brand = '',$drive = '',$title = '',$page = 1)
    {
        $map = [];
        if($type){
            $map['type'] = $type;
        }
        if($brand){
            $map['brand'] = $brand;
        }
        if($drive){
            $map['drive'] = $drive;
        }
        if($title){
            $map['title'] = ['like',"%{$title}%"];
        }
        $storetruck_list = $this->storetruck_model->where($map)->order('id DESC')->paginate(15, false, ['page' => $page]);
        return $this->fetch('index', ['storetruck_list' => $storetruck_list,'type' => $type,'brand' => $brand,'drive' => $drive,'title' => $title]);
    }
    /**
     * 添加
     * @return mixed
     */
    public function add()
    {
        return $this->fetch();
    }
    /**
     * 保存
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            $validate_result = $this->validate($data, 'StoreTruck');
            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                if(intval($data['low_price']) >= intval($data['price'])){
                    $this->error('降价必须小于原价');
                }
                if ($this->storetruck_model->allowField(true)->save($data)) {
                    $this->success('保存成功');
                } else {
                    $this->error('保存失败');
                }
            }
        }
    }
    /**
     * 编辑
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $storetruck = $this->storetruck_model->find($id);
        return $this->fetch('edit', ['storetruck' => $storetruck]);
    }
    /**
     * 更新
     * @param $id
     */
    public function update($id)
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            $validate_result = $this->validate($data, 'StoreTruck');
            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                if(intval($data['low_price']) >= intval($data['price'])){
                    $this->error('降价必须小于原价');
                }
                if ($this->storetruck_model->allowField(true)->save($data, $id) !== false) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            }
        }
    }
    /**
     * 删除
     * @param $id
     */
    public function delete($id = '', $ids = [])
    {
        $id = $ids ? array_merge($ids) : $id;
        if($ids){
            $i = 0;
            for(;$i<count($ids);$i++){
                $fileImg = $this->storetruck_model->where('id',$ids[$i])->value('photo');
                $ji = 0;
                for(;$ji<count($fileImg);$ji++){
                    if($fileImg[$ji]){
                       if(file_exists('./'.$fileImg[$ji])){
                           unlink('./'.$fileImg[$ji]);
                       }
                    }
                }
            }
        }
        if ($id) {
            if ($this->storetruck_model->destroy($id)) {
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        } else {
            $this->error('请选择需要删除的内容');
        }
    }
    /**
     * 文章审核状态切换
     * @param array  $ids
     * @param string $type 操作类型
     */
    public function toggle($ids = [], $type = '' ,$id = '')
    {
        $data   = [];
        $status = $type == 'audit' ? 1 : 0;
        if(!empty($id)){
            if($this->storetruck_model->save(['status'=>$status],['id'=>$id])){
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
        if (!empty($ids)) {
            foreach ($ids as $value) {
                $data[] = ['id' => $value, 'status' => $status];
            }
            if ($this->storetruck_model->saveAll($data)) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        } else {
            $this->error('请选择需要操作的文章');
        }
    }
    /**
     * 查看
     */
    public function reading($id)
    {
        $storetruck = $this->storetruck_model->find($id);
        return $this->fetch('reading', ['storetruck' => $storetruck]);
    }
}